$(function(){
	iFrmaeHight();
	$(window).resize(function(){
		iFrmaeHight();
	})
	function iFrmaeHight(){
		$('.yz-cont').height($('.staff-box-cont').height() - $('.s-b-c-top').height() - 20)
		
//		var editHeight = $('.staff-box-cont').height();
//		var boxTop = $('.s-b-c-cont').offset().top;
//		$('.p-c-l-box').height(editHeight - boxTop - 60);
//		$('.p-c-r-box').height(editHeight - boxTop - 60);
//		$('.power-line').height(editHeight - boxTop - 60);
//		$('.r-box-cont').height(editHeight - boxTop - 167);
//		var offEditTop = $('.s-e-b-cont').offset().top;
//		$('.s-e-b-cont').height(editHeight - offEditTop + 25);
	}
	$('.yz-add').on('click',function(){
		$('.yz-add-tk').show();
	})
	$('.tk-cont-qx').on('click',function(){
		$('#add-yz').val('');
		$('#add-pm').val('');
		$('.yz-add-tk').hide();
	})
	$('.tk-cont-qd').on('click',function(){
		var leftLi = $('.tk-cong-left').find('li');
		var rightLi = $('.tk-cong-right').find('li');
		var leftValue = $('#add-yz').val();
		var rightValue = $('#add-pm').val();
		var dataId = $('.tk-cong-left-add').attr('data-id');
		var leftFlag = true;
		var rightFlag = true;
		$('#add-yz').attr('disabled','disabled');
		$('#add-pm').attr('disabled','disabled');
		var length = $('.yz-cont-item').length + 1;
		if(leftValue != ''){
			$.each(leftLi, function(i,v) {
				if($(v).text() == leftValue){
					leftFlag = false;
				}
			});
			if(leftFlag){
				$('.tk-cong-left ul').append('<li data-id='+length+'>'+leftValue+'</li>');
			}
		}else{
			layer.msg('请选择业种',{icon:2});
			return false;
		}
		
		if(rightValue != ''){
			$.each(rightLi, function(i,v) {
				if($(v).text() == rightValue){
					rightFlag = false;
				}
			});
			if(rightFlag){
				$('.tk-cong-right ul').append('<li>'+rightValue+'</li>');
				layer.msg('添加成功',{icon:1})
				if(dataId){
					var str = `
						<div class="y-c-i-r-item-box">
							<div class="y-c-i-r-item">
								${rightValue}
								<div class="yz-close"><img src="images/u2089.png"></div>
							</div>
							<div class="y-c-i-r-item">
								<img src="images/zi_liao/u5505.png">
								<input type="file" name="" id="" value="">
							</div>
						</div>
					`;
					var obj = $('.yz-cont-item[data-id='+dataId+']');
					obj.find('.y-c-i-right').append(str);
					
				}else{
					var minHeight = 0;
					var heightArr = [];
					$.each($('.yz-cont-list'), function(i,v) {
						var theHeight = 0;
						$.each($(v).find('.yz-cont-item'), function(ii,vv) {
							theHeight += $(vv).height();
						});
						heightArr.push(theHeight);
					});
					var minIndex =  heightArr.indexOf(Math.min.apply(null, heightArr));
					var str = `
						<div class="yz-cont-item" data-id="${length}">
							<div class="y-c-i-title">
								${leftValue}
								<div class="yz-close"><img src="images/u2089.png"></div>
							</div>
							<div class="y-c-i-right">
								<div class="y-c-i-r-item-box">
									<div class="y-c-i-r-item">
										${rightValue}
										<div class="yz-close"><img src="images/u2089.png"></div>
									</div>
									<div class="y-c-i-r-item">
										<img src="images/zi_liao/u5505.png">
										<input type="file" name="" id="" value="">
									</div>
								</div>
							</div>
						</div>
					`;
					$('.yz-cont-list').eq(minIndex).append(str);
				}
			}else{
				layer.msg('重复添加',{icon:2})
			}
		}else{
			layer.msg('请选择品名',{icon:2});
			return false;
		}
		
		
		$('.yz-add-tk').hide();
	})
	// 点击图片上传资料
	$('.yz-cont').on('change','input',function(){
		console.log(this);
		var $file = $(this);
        var fileObj = $file[0];
        var windowURL = window.URL || window.webkitURL;
        var dataURL;
        var $img = $(this).siblings();
        if (fileObj && fileObj.files && fileObj.files[0]) {
            dataURL = windowURL.createObjectURL(fileObj.files[0]);
            $img.attr('src', dataURL);
        } else {
            dataURL = $file.val();
            var imgObj = document.getElementById("preview");
            // 两个坑:
            // 1、在设置filter属性时，元素必须已经存在在DOM树中，动态创建的Node，也需要在设置属性前加入到DOM中，先设置属性在加入，无效；
            // 2、src属性需要像下面的方式添加，上面的两种方式添加，无效；
            imgObj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
            imgObj.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = dataURL;

        }
	})
	$('.yz-cont').on('click','.yz-close',function(){
		var _this = this;
		layer.confirm('是否删除节点？', {
			icon: 3,
		    btn: ['确定','取消'] //按钮
		}, function(){
			// 删除大的
			if($(_this).parent().attr('class') == 'y-c-i-title'){
				$(_this).parents('.yz-cont-item').remove();
			//删除小的
			}else if($(_this).parent().attr('class') == 'y-c-i-r-item'){
				$(_this).parents('.y-c-i-r-item-box').remove();
			}
		  	layer.msg('删除成功', {icon: 1});
		}, function(){
		  
		});
		
	})
	var flag = true;
	var flag2 = true;
	$('.tk-cong-left .look-more').on('click',function(){
		var _this = this;
		if(flag){
			var height = 0;
			$.each($(_this).find('li'), function(i,v) {
				height += $(v)[0].clientHeight;
			});
			if(height > 500){
				height = 500;
			}
			$(_this).find('ul').height(height);
			flag = false;
		}else{
			$(_this).find('ul').height(0);
			flag = true;
		}
	})
	
	$('.tk-cong-right .look-more').on('click',function(){
		var _this = this;
		if(flag2){
			var height = 0;
			$.each($(_this).find('li'), function(i,v) {
				height += $(v)[0].clientHeight;
			});
			if(height > 500){
				height = 500;
			}
			$(_this).find('ul').height(height);
			flag2 = false;
		}else{
			$(_this).find('ul').height(0);
			flag2 = true;
		}
	})
	$('.tk-cong-left .look-more').on('click','li',function(){
		var value = $(this).text();
		var index = $(this).index();
		var dataId = $(this).attr('data-id');
		var itemArr = $('.yz-cont-item');
		console.log(dataId);
		$.each(itemArr, function(i,v) {
			if(dataId == $(v).attr('data-id')){
				$('.tk-cong-right-add ul').html('<li class="yz-add-btn">+新增</li>');
				var zheItem = $(v).find('.y-c-i-r-item-box');
				$.each(zheItem, function(ii,vv) {
					var textValue = $.trim($(vv).find('.y-c-i-r-item').eq(0).text());
					$('.tk-cong-right-add ul').append('<li>'+textValue+'</li>');
				});
			}
		});
		if(index == 0 && $(this).parents('.tk-cong-left-add').length > 0){
			$(this).parents('.look-more').siblings('input').removeAttr('disabled');
			$('.tk-cong-right-add ul').html('<li class="yz-add-btn">+新增</li>');
			$(this).parents('.tk-cong-left-add').removeAttr('data-id');
		}else{
			$(this).parents('.look-more').siblings('input').val(value);
			$(this).parents('.tk-cong-left-add').attr('data-id',dataId);
			$(this).parents('.look-more').siblings('input').attr('disabled','disabled');
		}
		$(this).parent().height(0);
		flag = true;
		flag2 = true;
		return false;
	})
	$('.tk-cong-right .look-more').on('click','li',function(){
		var value = $(this).text();
		var index = $(this).index();
		if(index == 0 && $(this).parents('.tk-cong-right-add').length > 0){
			$(this).parents('.look-more').siblings('input').removeAttr('disabled');
			
		}else{
			$(this).parents('.look-more').siblings('input').val(value);
			$(this).parents('.look-more').siblings('input').attr('disabled','disabled');
		}
		$(this).parent().height(0);
		flag = true;
		flag2 = true;
		return false;
	})
})
